package com.woniuxy.main.handler;


import com.woniuxy.main.pojo.Department;
import com.woniuxy.main.pojo.Roles;
import com.woniuxy.main.pojo.Staff;
import com.woniuxy.main.service.DepartmentService;
import com.woniuxy.main.service.RolesService;
import com.woniuxy.main.service.StaffService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/*
 *
 *  @author zhc
 *  @create 2019-07-31 18:42
 */
@Controller
@RequestMapping("/empl")
public class StaffHandler {
    @Autowired
    StaffService staffService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    RolesService rolesService;


    /*
   登录验证
    */
    @RequestMapping("/login")
    public ModelAndView StaffLogin(Staff staff, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        //System.out.println(".............." + staff);
        if (staff.getSaccount() == null || staff.getSpassword() == null) {

            modelAndView.setViewName("/index.html");
            return modelAndView;
        }
        modelAndView.addObject("staff",staff);

        /*
        shiro
         */
        String message = "";
        //1 获取subject
        Subject currentUser = SecurityUtils.getSubject();
        //2 开始认证（登录）操作，将用户名和密码传递给Token

        if (!currentUser.isAuthenticated()) { //如果没认证
            //封装成token
            UsernamePasswordToken token = new
                    UsernamePasswordToken(staff.getSaccount(), staff.getSpassword());
            //System.out.println("======================");

            /*if (staff.isRememberMe()){ //如果有勾选记住我
                token.setRememberMe(true);
            }*/

            try {
                //调用login 进行认证
                currentUser.login(token);

                request.getSession().setAttribute("staff",staff.getStaff_id());

                System.out.println("认证成功");
                modelAndView.setViewName("redirect:/frame.html");
                return modelAndView;
            } catch (UnknownAccountException e) {
                System.out.println("用户名不存在");
                message = "用户不存在";
            } catch (IncorrectCredentialsException e) {
                System.out.println("密码错误");
                message = "密码有误";
            } catch (LockedAccountException e) {
                System.out.println("账户被锁定异常");
                message = "账户被锁定";
            } catch (AuthenticationException e) {
                System.out.println("异常不详");
                e.printStackTrace();
                message = "不明异常，请联系管理员";
            }
            modelAndView.addObject("message",message);
            modelAndView.setViewName("/index.html");

            return modelAndView;
        }
        modelAndView.setViewName("redirect:/frame.html");
        return modelAndView;
    }

/*
查找所有
 */
    @GetMapping("/all")
    public String findAll(Model model){

        List<Department> departments = departmentService.findall();

        model.addAttribute("departments", departments);

        List<Roles> roles = rolesService.findall();

        model.addAttribute("roles", roles);

        List<Staff> staffs = staffService.findAll();

        model.addAttribute("staffs",staffs);

        //System.out.println( staffs);

        return "pagefile/system/StaffShow";

    }
    /*
    跳转添加员工页面
     */
    @GetMapping("/addBefore")
    public  String AddStaff1(Model model){

      List<Department> departments=departmentService.findall();

        model.addAttribute("departments",departments);

        List<Roles> roles = rolesService.findall();

        model.addAttribute("roles",roles);

        return "pagefile/system/StaffAdd";
    }

    /*
    账号重复判断
     */
    @RequestMapping("/sReapeated")
    @ResponseBody
    public String sReapeated(String saccount) {

        int b = staffService.sRepeated(saccount);

        if (b == 0) {

            return "0";

        } else {

            return "1";
        }
    }

    /*
    添加员工
     */
    @RequestMapping("/add")
    public  String Addasataff2(Staff staff){

        // System.out.println("2222"+staff);

        Boolean staff1 = staffService.addStaff(staff);


        if(staff1){
            return "redirect:all";
        }else {
            return "forward:addBefore";
        }

    }
    @RequestMapping("/findpass")
    @ResponseBody
    public String findpassById(Staff staff){

        //System.out.println(staff);

        boolean b=staffService.findpass(staff);

        if(b){
            return "0";
        }else{
            return "1";
        }

    }

    /*
    修改信息跳转
     */
    @RequestMapping("/update/{id}")
    public String updateStaff(@PathVariable("id") int id, Model model){

        List<Department> departments = departmentService.findall();

        model.addAttribute("departments", departments);

        List<Roles> roles = rolesService.findall();

        model.addAttribute("roles", roles);
        //System.out.println("id"+id);
        Staff staff=staffService.selectStaffById(id);

        model.addAttribute("staff", staff);

        return "pagefile/system/StaffUpdate";
    }

    /*
    修改信息
     */
    @RequestMapping("/update2")
    public String updateStaff2(Staff staff){

        boolean b=staffService.updateStaff(staff);

        System.out.println(staff);

        if (true) {
            return "redirect:all";
        }
        return  "";
    }

    @RequestMapping("/del/{id}")
    public String delStaff(@PathVariable("id") int id) {
        //System.out.println(id);
        boolean b = staffService.delStaff(id);

        if (b) {
            return "redirect:/empl/all";
        } else {
            return "";
        }

    }

    /*
    条件查找
     */
    @RequestMapping("/condition")
    public String findByCondition(Staff staff, Model model) {

        //System.out.println(staff);

        List<Staff> staffs = staffService.findByCondition(staff);

        model.addAttribute("staffs", staffs);

        List<Department> departments = departmentService.findall();

        model.addAttribute("departments", departments);

        List<Roles> roles = rolesService.findall();

        model.addAttribute("roles", roles);

        return "pagefile/system/StaffShow";
    }

}
